package eco.footprint;

import java.text.MessageFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class FootprintActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

		String hello = getResources().getString(R.string.strHello);
		TextView txtLampInfo = (TextView) findViewById(R.id.txtHello);
		txtLampInfo.setText(Html.fromHtml(hello), BufferType.SPANNABLE);
		txtLampInfo.setMovementMethod(LinkMovementMethod.getInstance());

		((RadioButton) findViewById(R.id.rbTestTypeSimple)).setChecked(true);

		Button btnAbout = (Button) findViewById(R.id.btnAbout);
		btnAbout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				PackageManager manager = FootprintActivity.this
						.getPackageManager();
				PackageInfo info = null;
				try {
					info = manager.getPackageInfo(FootprintActivity.this
							.getPackageName(), 0);
				} catch (NameNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String about = MessageFormat.format("{0} v.{1}", getString(R.string.app_name),
						info.versionName);

				Builder builder = new AlertDialog.Builder(
						FootprintActivity.this).setTitle(
						FootprintActivity.this.getResources().getString(
								R.string.app_name)).setMessage(about);
				AlertDialog dialog = builder.setIcon(R.drawable.ic_launcher)
						.setPositiveButton(R.string.lbClose, null).show();
				WindowManager.LayoutParams lp = dialog.getWindow()
						.getAttributes();
				lp.dimAmount = 0.0f;
				dialog.getWindow().setAttributes(lp);
				dialog.getWindow().addFlags(
						WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
			}
		});

		Button btnStart = (Button) findViewById(R.id.btnStart);
		btnStart.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent i = new Intent(FootprintActivity.this, FootprintTestActivity.class);
				i.putExtra("full_test", ((RadioButton) findViewById(R.id.rbTestTypeFull)).isChecked());
				startActivityForResult(i, 0);
			}
		});
	
		ImageButton mBtnSettings = (ImageButton)findViewById(R.id.btnSettings);
		mBtnSettings.setOnClickListener(new OnClickListener() {			
			public void onClick(View arg0) {
				// Launch Preference activity
				Intent i = new Intent(FootprintActivity.this, Preferences.class);
				startActivity(i);		
			}
		});
	}
	
	// Listen for results.
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK)
			finish();
	}
}
